<?php

abstract class BaseEntity
{
	protected $Data;
	/*add, edit, delete, remove, no_change*/
	protected $Status;
	
	public function __construct($dataset)
	{
		$this->Data = array();
		foreach($dataset as $key => $value){
			if(!is_numeric($key)){
				$this->Data[$key] = $value;
			}
		}
	}
	
	public function setStatus($status){
		if ($this->Status == 'delete' or $this->Status == 'remove'){
			$trace = debug_backtrace();
			trigger_error(
				'Object of id '.$this->Data['id'].' has been deleted.'.
				' in ' . $trace[0]['file'] .
				' on line ' . $trace[0]['line'],
				E_USER_NOTICE
			);
		}
		elseif ($this->Status == 'add'){
			if ($status == 'delete'){
				$this->Status = 'remove';
			}
		}
		else {
			$this->Status = $status;
		}
	}
	
	public function __set($name, $value)
    {
        if ($name = 'id'){
			$tempArr = array($name=>$value);
			$this->Data = $tempArr + $this->Data;
		} else {
			$this->Data[$name] = $value;
			$this->setStatus('edit');
		}
    }

    public function __get($name)
    {
        if (array_key_exists($name, $this->Data)) {
            return $this->Data[$name];
        }
		if ($name == "Status"){
			return $this->Status;
		}
		
		if ($name == "Data"){
			return $this->Data;
		}

        $trace = debug_backtrace();
        trigger_error(
            'Undefined property via __get(): ' . $name .
            ' in ' . $trace[0]['file'] .
            ' on line ' . $trace[0]['line'],
            E_USER_NOTICE);
        return null;
    }
}


?>